package com.tuling.jvm;

/**
 * Created by chenhs on 2018/8/3.
 *
 * 【虚拟机栈和本地方法栈OOM测试（仅作第一点测试）】
 * VM Args：-Xss128k
 *
 * 如果线程请求的栈深度大于虚拟机所允许的最大深度，将抛出StackOverflowError
 * 如果虚拟机在扩展栈时无法申请到足够的内存空间，将抛出OutOfMemoryError
 *
 */
public class JavaVMStackSOF {

    private int stackLenth = 1;

    public void stackLeak() {
        stackLenth++;
        stackLeak();
    }

    public static void main(String[] args) {
        JavaVMStackSOF oom = new JavaVMStackSOF();

        try {
            oom.stackLeak();
        } catch (Throwable e) {
            System.out.println("stack length: " + oom.stackLenth);
            try {
                throw e;
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

        /**

         stack length: 989
         Exception in thread "main" java.lang.StackOverflowError

         */
    }
}

